<?php
	function __construct(){
		require './model/user_model.php';
		$_GET['a']();
	}
	//---------------------------------------
	function login(){
		if(isset($_SESSION['userInfo']) && $_SESSION['userInfo']){
			echo "<meta http-equiv=refresh content='0,./index.php' />";	
		}else{
			require './view/user/login.php';
		}	
	}
	function regin(){
		require './view/user/regin.php';
	}
	
	function dologin(){
		//先验证验证码是否匹配
		if(strtolower($_SESSION['code'])!=strtolower($_POST['code'])){
			echo '验证码不匹配!';
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=login' />";
            return;
		}
        //再验证用户名或密码是否匹配
		$condition = " WHERE uname='$_POST[uname]' AND upwd=md5('$_POST[upwd]')";
		if($userInfo = select($condition)){
			$_SESSION['userInfo'] = $userInfo[0];
			if(isset($_SESSION['goOrders'])){
				echo "<meta http-equiv=refresh content='0,./index.php?c=orders&a=info' />";
			}else{
				echo "<meta http-equiv=refresh content='0,./index.php?c=index&a=index' />";
			}
			
		}else{
			echo '帐号或密码错误!';
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=login' />";		
		}	
	}
	function loginout(){
		session_destroy();
		echo "<meta http-equiv=refresh content='0,./index.php?c=index&a=index' />";
	}
	
	function doregin(){
		//先验证验证码是否匹配
		if(strtolower($_SESSION['code'])!=strtolower($_POST['code'])){
			echo '验证码不匹配!';
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=regin' />";
            return;
		}
		if($_POST['upwd']!=$_POST['upwd2']){
			echo '密码不一致!';
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=regin' />";
			return;
		}
		unset($_POST['code']);
		unset($_POST['upwd2']);
		$_POST['upwd'] = md5($_POST['upwd']);

		if(insert()){
			sleep(0.5);
			echo '注册成功!';
			//--------
			$condition = " WHERE uname='$_POST[uname]' AND upwd='$_POST[upwd]'";
			$userInfo = select($condition);
			$_SESSION['userInfo'] = $userInfo[0];
			//--------
			echo "<meta http-equiv=refresh content='0,./index.php?c=index&a=index' />";
			
		}else{
			echo '注册失败!';
			echo "<meta http-equiv=refresh content='1,./index.php?c=user&a=regin' />";
		}
	}
